/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Aug 2, 2009 3:40:14 PM $
 *
 * Author: Eric Yang
 * Date  : Aug 2, 2009 3:40:14 PM
 *
 */
package com.gooex.domain.organization;

import java.io.Serializable;
import java.util.Date;

import org.c4j.encode.EncodeTools;
import org.c4j.string.StringTools;

/**
 * User
 * 
 * @author Eric Yang
 * @version 1.0
 */
public class User implements Serializable {
	/** PK */
	private String id;

	/** 邮箱(作为登陆账号) */
	private String email;

	/** 用户(用于显示) */
	private String name;

	/** 姓 */
	private String firstName;

	/** 全名 */
	private String fullName;

	/** 密码 */
	private String password;

	/** 状态 */
	private UserStatus status = UserStatus.ACTIVE;

	/** 用户照片地址 */
	private String image;

	/** 用户角色 */
	private Role role;

	/** 最后登录ip */
	private String lastLoginIP;

	/** 最后登录时间 */
	private String lastLoginTime;

	/** 登陆次数 */
	private long loginTimes;

	/** 创建时间 */
	private Date creationDate;

	/** 覆盖equal方法 */
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (obj instanceof User) {
			User user = (User) obj;
			if (id.equals(user.getId()) || name.equals(user.getName()) || email.equals(user.getEmail()))
				return true;
		}
		return false;
	}

	public boolean authorize(String pass) {
		if (pass.length() != EncodeTools.HASH_LENGTH)
			pass = EncodeTools.hash(pass);
		return password.equals(pass);
	}

	/**
	 * TODO to complete
	 */
	public void copyChangeableInfo(User user) {
		this.fullName = user.getFullName();
	}

	public boolean isSystemUser() {
		return false;
		// TODO
		// return AuthorityContext.isSystemUser(this);
	}

	/* ============setter/getter============================ */
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		if (StringTools.isBlankOrNull(name))
			return email.substring(0, email.indexOf("@"));
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		if (StringTools.isBlankOrNull(password)) {
			this.password = null;
			return;
		}
		if (password.length() != EncodeTools.HASH_LENGTH)
			password = EncodeTools.hash(password);
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public UserStatus getStatus() {
		return status;
	}

	public void setStatus(UserStatus status) {
		this.status = status;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getLastLoginIP() {
		return lastLoginIP;
	}

	public void setLastLoginIP(String lastLoginIP) {
		this.lastLoginIP = lastLoginIP;
	}

	public String getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(String lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public long getLoginTimes() {
		return loginTimes;
	}

	public void setLoginTimes(long loginTimes) {
		this.loginTimes = loginTimes;
	}

	public String getImgage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getImage() {
		return image;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

}
